<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>将Get请求转为Post请求</title>
</head>

<body style="text-align: center;">
	<form id="form1" name="form1" method="post"
		action="http://localhost/feiyoung-platform-openapi-server/rest/login">
		<p>
		将Get请求转为Post请求
		</p>
		<p>
			<textarea id="url" rows="20" cols="140" placeholder="请求地址及参数"></textarea>
		<div id="paramDiv">
			<input type="hidden" name="" value=""/>
		</div>
		</p>
		<br/>
		<hr/>
		<br/>
		<input type="button" value="提交" onclick="tijiao()" style="width: 100px; height: 50px;" />
		<input type="button" value="清除缓存" onclick="clearCache()" style="width: 100px; height: 50px;" />
		<div id="hisList"><div>
	</form>
</body>
<script type="text/javascript">
	
	loadUrlsFromCache();
	
	function loadUrlsFromCache(){
		var cache = null;
		try {
			cache = JSON.parse(localStorage.getItem('urls'));
		} catch(err) {}
		console.info("cache=" + cache);
		if (cache == undefined || cache == null) {
			cache = new Array();
		}
		var urlHtml='';
		cache.forEach(function(url){
			urlHtml='<a onclick="hisSub(\''+url+'\')">'+url+'</a><br/>'+urlHtml;
		});
		document.getElementById("hisList").innerHTML=urlHtml;
		document.getElementById("url").value=cache[cache.length-1];
	}
	
	function clearCache(){
		localStorage.setItem('urls', null);
		loadUrlsFromCache();
	}
	
	function hisSub(url) {
		document.getElementById("url").value=url;
		tijiao();
	}
	
	function putUrlToCache(url) {
		var cache = null;
		try {
			cache = JSON.parse(localStorage.getItem('urls'));
		} catch(err) {}
		console.info("cache=" + cache);
		if (cache == undefined || cache == null) {
			cache = new Array();
		}
		if (cache.indexOf(url) != -1) {
			console.info(url + '已存在，不再新增');
		} else {
			cache.push(url);
			localStorage.setItem('urls', JSON.stringify(cache));
		}
		console.info("putUrlToCache urls=" + cache);
	}
	
	function tijiao() {
		var url=document.getElementById("url").value;
		if(url!=null&&url.length>0){
			putUrlToCache(url);
		}
		document.form1.action = url;
		var index = url.indexOf('?');
		var query = url.substring(index + 1);
		var pairs = query.split('&');
		var html="";
		for (var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			// 如果没找到，就跳过
			if (pos == -1) {
				continue;
			} 
			var name = pairs[i].substr(0, pos); // 获得名称
			var value = pairs[i].substr(pos + 1); // 提取value
			value = decodeURIComponent(value); // 解码value ,如果地址栏的参数没有转码的话则不需要这一步
			html=html+'<input type="hidden" name="'+name+'" value="'+value+'"/>';
		}
		document.getElementById("paramDiv").innerHTML=html;
		document.form1.submit();
	}
</script>
</html>